package com.abc.robot01.room

import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.abc.robot01.room.bean.SystemMessageBean
import com.abc.robot01.room.dao.SystemMessageDao
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class SystemMessageDaoTest {
    private lateinit var systemMessageDao: SystemMessageDao
    private lateinit var db: AppDatabase

    @Before
    fun setUp() {
        val context = ApplicationProvider.getApplicationContext<Context>()
        db = Room.inMemoryDatabaseBuilder(
            context, AppDatabase::class.java).build()
        systemMessageDao = db.systemMessageDao()
    }

    @Test
    @Throws(Exception::class)
    fun addAnswer(){
        addData()
        val chatList = systemMessageDao.queryAll()
        Assert.assertNotNull(chatList)
        Assert.assertEquals(chatList.size, 2)
    }

    private fun addData() {
        val q1= SystemMessageBean().apply {
            success = true
            name = "01"
        }
        val q2 = SystemMessageBean().apply {
            success = false
            name = "02"
        }
        systemMessageDao.insert(q1)
        systemMessageDao.insert(q2)
    }


    @Test
    @Throws(Exception::class)
    fun update(){
        addData()
        val chatList = systemMessageDao.queryAll()
        Assert.assertNotNull(chatList)
        val id = chatList[0].id
        Assert.assertEquals(chatList.size, 2)
        systemMessageDao.update(chatList[0].apply {
            name = "u01"
        })
       val a =  systemMessageDao.queryById(id)
        Assert.assertEquals(a.name, "u01")
    }

    @Test
    @Throws(Exception::class)
    fun delete(){
        addData()
        val chatList = systemMessageDao.queryAll()
        Assert.assertNotNull(chatList)
        Assert.assertEquals(chatList.size, 2)
        val code = systemMessageDao.deleteAll()
        Assert.assertNotNull(code == 2)
        val resultList = systemMessageDao.queryAll()
        Assert.assertNotNull(resultList)
        Assert.assertEquals(resultList.size, 0)
    }

}